var https = require('https');
var http = require('http');
var async =  require('async');

var cheerio = require('cheerio'); // Basically jQuery for node.js

var options = {
    uri: '',
    transform: function (body) {
        return cheerio.load(body);
    }
};

function getApi(x,callback){

    // 判断是否为https
    var sendStpe = http;
    isDouyu =  typeof isDouyu == "undefined" ? false : true;

    if (x.indexOf("https://") > -1) {
        sendStpe = https;
    }

    //采用http模块向服务器发起一次get请求
    sendStpe.get(x, function (req, res) {
        var html = '';        //用来存储请求网页的整个html内容

        req.setEncoding('utf-8'); //防止中文乱码
        //监听data事件，每次取一块数据
        req.on('data', function (chunk) {
            html += chunk;
        });
        //监听end事件，如果整个网页内容的html都获取完毕，就执行回调函数
        req.on('end', function () {
            var $ = cheerio.load(html); //采用cheerio模块解析html

            var href = $('.Title-anchorName').attr('href');

            console.log(href);

            return href;

        });
    }).on('error', function (err) {
        console.log(err);
    });



}


var getUrl = ['https://www.douyu.com/74960','https://www.douyu.com/9999','https://www.douyu.com/82088'];
var roomArr = [];

//
// 循环调用
var test1 = function getData(callback) {

    for (var i=0;i<getUrl.length;i++){
        getApi(getUrl[i],function (roomObj) {
            roomArr.push(roomObj);
            callback(roomObj);
        });

    }

};


var aa = async.series([test1],function (data) {

    console.log(roomArr);
});

console.log('aa');
console.log(aa);

